package hoang.android.app.ui;

import hoang.android.app.MyApplication;
import hoang.android.app.R;
import hoang.android.app.utility.SmartDialogUtility;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class BaseActivity extends Activity implements OnClickListener {

	protected TextView titleHeader;
	protected ImageButton btHome;
	protected Context baseActivity;

	protected ProgressDialog progressDialog;

	protected MyApplication application;

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		int SDK_INT = android.os.Build.VERSION.SDK_INT;
		if (SDK_INT > 8) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.base_activity);

		application = (MyApplication) getApplication();
		baseActivity = BaseActivity.this;
		loadUI();
	}

	protected void loadUI() {
		titleHeader = (TextView) findViewById(R.id.tv_title);
		btHome = (ImageButton) findViewById(R.id.btHome);
		btHome.setOnClickListener(this);
	}

	/**
	 * setup thanh header
	 * 
	 * @param isShow
	 *            : an, hien header (true: hien)
	 * @param title
	 *            :
	 */
	protected void setHeader(boolean isShow, String title) {
		if (isShow) {
			titleHeader.setVisibility(View.VISIBLE);
			btHome.setVisibility(View.VISIBLE);
			btHome.setEnabled(true);
			titleHeader.setText(title);
		} else {
			titleHeader.setVisibility(View.INVISIBLE);
			btHome.setVisibility(View.INVISIBLE);
			btHome.setEnabled(false);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btHome:
			this.finish();
			startActivity(new Intent(baseActivity, FirstActivity.class));
			break;

		default:
			break;
		}
	}

	/**
	 * xay dung phuong thuc de hien thi progress khi load data
	 * 
	 * @param text
	 */
	@SuppressWarnings("static-access")
	public void showProgressDialog(String text) {
		if (progressDialog == null) {
			progressDialog = progressDialog.show(baseActivity, null, text);
			progressDialog.setCancelable(false);
		}
	}

	public void hideProgressDialog() {
		if (progressDialog != null) {
			progressDialog.cancel();
			progressDialog = null;
		}
	}

	// -----------------Custom Dialog----------------------------//

	public void showPositiveCustomDialog(Context context, int title,
			int message, int iconPositive) {

		SmartDialogUtility dialog = new SmartDialogUtility(context, title,
				message, iconPositive);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = LayoutParams.WRAP_CONTENT;
		lp.height = LayoutParams.WRAP_CONTENT;
		dialog.show();
		dialog.getWindow().setAttributes(lp);
	}

	
	public void showPositiveCustomDialog(Context context, String title,
			String message, int iconPositive) {

		SmartDialogUtility dialog = new SmartDialogUtility(context, title,
				message, iconPositive);
		
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = LayoutParams.WRAP_CONTENT;
		lp.height = LayoutParams.WRAP_CONTENT;
		dialog.show();
		dialog.getWindow().setAttributes(lp);
	}
}
